#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=Flash Drive Backup.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

$version = "0.Echo"

;TrayTip("Flash Drive Backup","Started " & $version & "-" & checkForNewVersion(),1)
TrayTip("Flash Drive Backup","Started...",1)
init()
checkForNewVersion()
While True
	backup()
	Sleep(3000)
	;Exit
WEnd

Func backup()
	$iniFile = @ScriptDir & "\FlashDriveBackup.cfg"
	$BackupFolderName = "\Flash Drive Backups\"
	$driveLetters = ""
	$driveIn = False
	$driveLetters = DriveGetDrive("REMOVABLE")
	If @error == 1 Then
		$driveIn = False
		$detected = True
		Sleep(1)
	Else
		If $detected == True Then $driveIn = True
		If $driveIn == True Then
			TrayTip("Flash Drive Backup","Backing Up Drive...",1)
			;MsgBox(0,"","Detected")
			$detected = False
		EndIf
		$flashDrives = $driveLetters
		If FileExists($iniFile) Then
			$flashDrives = IniReadSectionNames($iniFile)
		EndIf
		For $i = 1 To $flashDrives[0]
			If $flashDrives[$i] == "Setup" Then
				Sleep(1)
			Else
				If FileExists($iniFile) Then
					$serial = $flashDrives[$i]
					$name = IniRead($iniFile,$flashDrives[$i],"name","Unknown")
					$letter = findDrive($flashDrives[$i])
					$incremental = IniRead($iniFile,$flashDrives[$i],"incremental",0)
					IniWrite($iniFile,$flashDrives[$i],"letter",$letter)
					$backupPath = IniRead($iniFile,"Setup","localcache",@UserProfileDir & $BackupFolderName & $serial)
				Else
					$serial = DriveGetSerial($flashDrives[$i] & "\")
					$letter = $flashDrives[$i]
					$incremental = 1
					$backupPath = @UserProfileDir & $BackupFolderName & $serial
					If FileExists($backupPath) Then
						Sleep(1)
					Else
						TrayTip("Flash Drive Backup","Found New Drive " & $serial,1)
					EndIf
				EndIf
				$backupPath = @UserProfileDir & $BackupFolderName & $serial
				If $incremental == 0 Then
					If FileExists($backupPath) Then
						mirror($letter & "\",$backupPath)
					Else
						DirCreate($backupPath)
						dirIcon(@UserProfileDir & $BackupFolderName,"Flash")
						dirIcon($backupPath, "USB")
						mirror($letter & "\",$backupPath)
					EndIf
				Else
					If FileExists($backupPath) Then
						incmirror($letter & "\",$backupPath)
					Else
						DirCreate($backupPath)
						dirIcon(@UserProfileDir & $BackupFolderName,"Flash")
						dirIcon($backupPath,"USB")
						incmirror($letter & "\",$backupPath)
					EndIf
				EndIf
			EndIf
		Next
	EndIf
EndFunc

Func findDrive($serial)
	Local $aArray = DriveGetDrive("REMOVABLE")
	If @error Then
		;MsgBox(4096, "DriveLock", "It appears an error occurred.")
		Sleep(5000)
	Else
		$driveIn = False
		For $i = 1 To $aArray[0]
			$driveLetter = StringUpper($aArray[$i])
			$drive = DriveGetSerial($driveLetter)
			If $serial == $drive Then
				Return $driveLetter
			Else
				Sleep(1)
			EndIf
		Next
	EndIf
EndFunc

Func mirror($from,$to)
	RunWait('robocopy /mir /fft /z /xa:h /w:1 ' & $from & ' "' & $to & '"',"",@SW_HIDE)
EndFunc


Func incmirror($from,$to)
	$time = @YEAR & "-" & @MON & "-" & @MDAY
	$to = $to & "\" & $time
	DirCreate($to)
	RunWait('robocopy /s /r:1 /w:1 /maxage:1 ' & $from & ' "' & $to & '"',"",@SW_HIDE)
	dirIcon($to,"Clock")
EndFunc

Func dirIcon($folder,$icon)
	IniWriteSection($folder & "\" & "Desktop.ini", ".ShellClassInfo", _
					"IconFile=" & @AppDataDir & "\Icons\" & $icon & ".ico" & @LF & _
					"IconIndex=0" & @LF & _
					"InfoTip=By Jo")
	FileSetAttrib($folder, "+R")
	FileSetAttrib($folder & "\" & "Desktop.ini", "+HSA")
EndFunc

Func init()
	Global $detected = False
	DirCreate(@AppDataDir & "\Icons\")
	FileInstall(".\icon.ico",@AppDataDir & "\Icons\USB.ico",1)
	FileInstall(".\usb.ico",@AppDataDir & "\Icons\Flash.ico",1)
	FileInstall(".\clock.ico",@AppDataDir & "\Icons\Clock.ico",1)
EndFunc

Func checkForNewVersion()
	$versionURL = "https://flash-drive-backup.googlecode.com/hg/current.version"
	If @error Then
		Sleep(1)
	Else
		$installerURL = "https://flash-drive-backup.googlecode.com/hg/Install%20FDBU.exe"
		$serverVersion = BinaryToString(InetRead($versionURL))
		If $serverVersion == $version Then
			Sleep(1)
		Else
			Return $serverVersion
			InetGet($installerURL,"Update Flash Drive Backup.exe")
			RunWait("Update Flash Drive Backup.exe")
			Sleep(30000)
			TrayTip("Flash Drive Backup","FDBU Needs to update...",1)
		EndIf
	EndIf
EndFunc
